using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.AutoMapperExtend;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Mvc.Api.sys
{
    /// <summary>
    /// FX_Level
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
  //  [Authorize]//验证是否登录
    public class FL_FileLevelController : CoreApiController
    {
        private readonly FL_FileLevelService _service;
        private readonly IDataDicHelp _dataDicHelp;
        CoreDBContext _coreDBContext;
        SQLHelper _sQLHelper;
        /// <summary>
        /// FX_Level
        ///</summary>
        public FL_FileLevelController(FL_FileLevelService service, CoreDBContext coreDBContext, SQLHelper sQLHelper)
        {
            _service = service;
            _coreDBContext = coreDBContext;
            _sQLHelper = sQLHelper;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        //[PermissionFilter(nameof(FL_FileLevelController) + "_" + nameof(FL_FileLevelController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<FL_FileLevelDto>
                pagingQueryModel)
        {
            IPageModel<FL_FileLevelDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        //[PermissionFilter(nameof(FX_LevelController) + "_" + nameof(FX_LevelController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(FL_FileLevelDto dto)
        {
           
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                dto.CreateUser = UserIdentity.Id;
                dto.CreateTime = System.DateTime.Now;
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                //dto.CreateUser = UserIdentity.Id;
                //dto.CreateTime = System.DateTime.Now;
                //await _service.UpdateAsync(dto);

                string SQLstr = @"UpDate  FL_FileLevel set  CreateUser='"+ UserIdentity.Id + "'," +
                    "CreateTime ='"+ System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "'," +
                    "FileLeve='"+ dto.FileLeve+ "'," +
                    "Remarks='"+ dto .Remarks+ "' where ID='"+ dto.Id+ "'";

                SqlParameter[] param1 = new SqlParameter[] { };
                List<SqlParameter> sqlParameterList1 = new List<SqlParameter>();
                param1 = sqlParameterList1.ToArray();
            
                var dataTable = _sQLHelper.ExecuteNonQuery(SQLstr, param1);

                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        //[PermissionFilter(nameof(FX_LevelController) + "_" + nameof(FX_LevelController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {
            List<Guid> newList = new List<Guid>();
            for (int i = 0; i < ids.Count(); i++)
            {
                if (_coreDBContext.FL_FileLevel.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
                {
                    newList.Add(ids[i]);
                }
            }
            await _service.DeleteBatchAsync(newList.ToArray());

            return this.JsonSuccess();

            //await _service.DeleteBatchAsync(ids);
            //return this.JsonSuccess();
        }


        ///// <summary>
        ///// 根据ID获取信息
        /////</summary>
        ///// <param name="id"></param>
        ///// <returns></returns>

        //[HttpGet("Get")]
        //public async Task<IActionResult>
        //    Get(Guid id)
        //{
        //    var model = await _service.GetAsync(id);
        //    return this.JsonSuccess(model);
        //}


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        //[PermissionFilter(nameof(FX_LevelController) + "_" + nameof(FX_LevelController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> Export(PagingModel<FL_FileLevelDto> pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion


        #region 扩展

        ///// <summary>
        ///// /根据一组字典类型category字段,返回多个以category作为KEY的集合
        ///// </summary>
        ///// <param name="categoryCodArray"></param>
        ///// <returns></returns>
        //[HttpGet("GetDicValue")]
        //[PermissionFilter(nameof(FX_LevelController) + "_" + nameof(FX_LevelController.GeDataDicValueByCategory), AuthorizationConst.GET)]
        //public IActionResult GeDataDicValueByCategory(string[] categoryCodArray)
        //{
        //    var ret = _dataDicHelp.GeDataDicValueByCategory(categoryCodArray);
        //    return this.JsonSuccess(ret);
        //}

        #endregion


    }
}
